<?php
error_reporting(E_ALL);

echo "/*";

// Apro file per verificare esecuzione necessaria
define('FAP_CRON_FILE', dirname(__FILE__) . "/cron.txt");
$nextrun = file_get_contents(FAP_CRON_FILE);

// Se necessaria esecuzione
if($nextrun < time()) {
		
	// Includo Joomla
	define( '_JEXEC', 1 );
	define( '_VALID_MOS', 1 );
	define('JPATH_BASE', dirname(__FILE__) . "/../../../../.." );
	define( 'DS', DIRECTORY_SEPARATOR );

	require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
	require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
	
	// Init application
	$mainframe =& JFactory::getApplication('site');
	
	/**
	 * CREATE THE APPLICATION
	 *
	 * NOTE :
	 */
	define('_ISO','charset=utf-8');
	jimport('joomla.database.database');
	jimport( 'joomla.database.table' );
	require_once(JPATH_BASE . '/plugins/system/legacy/dbtable.php');
	
	require_once(JPATH_BASE . '/configuration.php');
	$conf = new JConfig();
	
	$host 		= $conf->host;
	$user 		= $conf->user;
	$password 	= $conf->password;
	$database	= $conf->db;
	$prefix 	= $conf->dbprefix;
	$driver 	= $conf->dbtype;
	$debug 		= $conf->debug;

	$options	= array ( 'driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix );
	
	$database =& JDatabase::getInstance( $options );
	
	// Carico dati segnalazioni limitatamente
	// alle tipologie 1 e 2, confermate, pubblicate e non risolte
	jimport('joomla.database.database');
	$query = "SELECT * FROM #__fap_report_allarm WHERE (report_type='1' OR report_type='2') AND confirmed='1' AND published='1' AND report_status != '1' ORDER BY report_address, report_number, report_zip, recordtime DESC";
	$database->setQuery( $query );
	$data = $database->loadObjectList();
	
	// Includo libreria excel
	require_once JPATH_BASE.'/administrator/components/com_chronocontact/excelwriter/Writer.php';
	
	// Catturo output
	ob_start();
	
	// Init excel	
	$xls = new Spreadsheet_Excel_Writer();
	$xls->setVersion(8); // this fixes the 255 limit issue! :)
	
	// Formats
	$format_title =& $xls->addFormat();
	$format_title->setBold();
	
	// Sheet
	$sheet =& $xls->addWorksheet('segnalazioni');
	$sheet->setInputEncoding('utf-8');
	
	// Colonne
	$cols = array(
		"ID",
		"Nome",
		"Cognome",
		"Email",	
		"Indirizzo",
		"Civico",
		"CAP",
		"Descrizione",
		"Tipologia",
		"Stato",
		"Data segnalazione",
		"Data presa consegna",
		"Note"
	);
	
	// Stampo titoli di colonna
	$row = 0;
	foreach($cols as $index => $col){
		$sheet->writeString($row, $index, $col, $format_title);
	}
				
	// Stampo celle
	$row++;
	foreach($data as $s) {
		
		$sheet->writeString($row, 0, $s -> id);
		$sheet->writeString($row, 1, $s -> name);
		$sheet->writeString($row, 2, $s -> surname);
		$sheet->writeString($row, 3, $s -> email);
		$sheet->writeString($row, 4, $s -> report_address);
		$sheet->writeString($row, 5, $s -> report_number);
		$sheet->writeString($row, 6, $s -> report_zip);
		$sheet->writeString($row, 7, $s -> report);
		$sheet->writeString($row, 8, "S" . $s -> report_type);
		$sheet->writeString($row, 9, $s -> report_status);
		if($s -> recordtime) {
			$sheet->writeString($row, 10, date("d-m-y", strtotime($s -> recordtime)));
		} else {
			$sheet->writeString($row, 10,"");
		}
	
		if($s -> report_recovery_date && $s -> report_recovery_date != "0000-00-00") {
			$sheet->writeString($row, 11, date("d-m-y", strtotime($s -> report_recovery_date)));
		} else {
			$sheet->writeString($row, 11,"");
		}
	
		$sheet->writeString($row, 13, $s -> report_note);
		
		// Avanzo riga
		$row++;
	}
	
	$xls->close();
	
	// Catturo out excel
	$filename = dirname(__FILE__) . "/segnalazione_dissesti_" . date("Y_m_d") . ".xls";
	$content = ob_get_contents();
	ob_clean();
	
	// Salvo file e rimuovo eventuale copia presente
	if(is_file($filename)) {
		unlink($filename);
	}
	
	file_put_contents($filename, $content);
	
	// Preparo mail da inviare
	$conf	=& JFactory::getConfig();
	$from = $conf->getValue('config.mailfrom');
	$fromname = $conf->getValue('config.fromname');
	$to = "sarrubbi_napolipark@libero.it";
	$bcc = "diego@infocube.it";
	$subject = "Segnalazione dissesti del " . date("d-m-Y");
	$body = ""; 
	echo "da " . $from . " a " . $to . "<br/>";
	
	// Invio email
	if (!JUtility::sendMail($from, $fromname, $to, $subject, $body, 0, null, $bcc, $filename)) {
		echo "ERRORE! Mail non inviata!<br/>";
	
	} else {
	
		echo "OK! Mail inviata correttamente!<br/>";
		
		// Aggiorno file per segnalare prossimo invio
		// tra 7 giorni
		$nextrun = (time() + 7 * 24 * 60 * 60);
		file_put_contents(FAP_CRON_FILE, $nextrun);
		echo "Prossimo invio il " . date("d-m-Y", $nextrun);
		
		
	}
}

echo "*/";